var scalebar1 = null;
var defaultPointIcon = "../img/defaultLocation.png";

var arcGisUrl = "http://39.107.125.44:6080";
var eventMap = null;
var centerX = 106.638;// 120.206354450751;
var centerY = 26.684;// 30.0725834699399;
var drawHandler;
var vec;
var veca;

require([ "esri/map", "esri/Color", "esri/graphic", "esri/InfoTemplate",
		"esri/SpatialReference", "esri/renderers/HeatmapRenderer",
		// 控件
		"esri/dijit/OverviewMap", "esri/dijit/Scalebar",
		"esri/dijit/Measurement", "esri/units",
		// 图层
		"tdlib/TDTLayer", "tdlib/TDTAnnoLayer", "tdlib/TDT_ImgLayer",
		"tdlib/TDT_ImgAnnoLayer", "esri/layers/GraphicsLayer",
		"esri/layers/ArcGISDynamicMapServiceLayer",
		"esri/layers/ArcGISTiledMapServiceLayer", "esri/layers/FeatureLayer",
		// 图形要素
		"esri/geometry/Point", "esri/geometry/Polyline",
		"esri/geometry/Polygon", "esri/geometry/Circle",
		// 样式
		"esri/symbols/TextSymbol", "esri/symbols/SimpleMarkerSymbol",
		"esri/symbols/PictureMarkerSymbol", "esri/symbols/SimpleLineSymbol",
		"esri/symbols/SimpleFillSymbol", "esri/toolbars/draw" ], function(Map,
		Color, Graphic, InfoTemplate, SpatialReference, HeatmapRenderer,
		OverviewMap, Scalebar, Measurement, Units, TDTLayer, TDTAnnoLayer,
		TDT_ImgLayer, TDT_ImgAnnoLayer, GraphicsLayer,
		ArcGISDynamicMapServiceLayer, ArcGISTiledMapServiceLayer, FeatureLayer,
		Point, Polyline, Polygon, Circle, TextSymbol, SimpleMarkerSymbol,
		PictureMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, Draw) {

	eventMap = new Map("eventmap", {
		slider : false,
		logo : false,
		showLabels : true,
		center : [ centerX, centerY ],
		zoom : 17,
		minZoom : 8
	});

	scalebar1 = new Scalebar({
		map : eventMap,
		scalebarUnit : "dual",
		attachTo : 'top-center'
	});

	scalebar1.hide();
	drawHandler = Draw;
	vec = new TDTLayer();// 切片线画图
	veca = new TDTAnnoLayer();// 切片标注
	var img = new TDT_ImgLayer();
	var imga = new TDT_ImgAnnoLayer();
	eventMap.addLayer(vec);
	eventMap.addLayer(veca);

	clearInterval(flashTime);
	// 单位数据刷新
	var flashTime = setInterval(function() {
		refreashFlickerPoint();
	}, 75);
	
	
	eventMap.on("mouse-down", function(evt) {
		console.log("graphicslayer:mouse-down");
		flickerPoint.setGeometry(evt.mapPoint);
		locationPoint.setGeometry(evt.mapPoint);
		setPoint(evt.mapPoint);
	});
});

function setPoint(point){
	setAutoLocation(1);
	var lat =point.y;
	var lng = point.x;
	$("#lng2").val(lng);
	$("#lat2").val(lat);
	$("#lng").val(lng);
	$("#lat").val(lat);
	var bd = wgs2bd(lat,lng);
	var pt = new BMap.Point(bd[1], bd[0]);
	// 创建一个地理位置解析器
	var geoc = new BMap.Geocoder();
	geoc.getLocation(pt,function(rs) {// 解析格式：城市，区县，街道
		var addComp = rs.addressComponents;
		var html = addComp.province + ", " + addComp.city + ", "
			+ addComp.district + ", " + addComp.street + ", "
			+ addComp.streetNumber
		jQuery("#locationDes2").html(html);
		jQuery("#locationDes").html(html);
		jQuery("#mapLocationDes").html(html);
	});
}
function refreashFlickerPoint() {
	if (flickerPoint) {
		var symbol = flickerPoint.symbol;

		symbol.color.a -= 0.05;
		symbol.setSize(symbol.size + 8);

		if (symbol.color.a <= 0) {
			symbol.color.a = 1;
			symbol.setSize(1);
		}
		flickerPoint.setSymbol(symbol);
	}
}
var flickerPoint;
var locationPoint;

function createFlickerPoint(data, str) {

	var symbol = new esri.symbol.SimpleMarkerSymbol(
			esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, // 样式,STYLE_CIRCLE(●),STYLE_CROSS(+),STYLE_DIAMOND(◆),STYLE_SQUARE(■),STYLE_X(X)
			1, // 像素
			new esri.symbol.SimpleLineSymbol(
					esri.symbol.SimpleLineSymbol.STYLE_SOLID, // 样式,STYLE_DASH(破折号),STYLE_DASHDOT(点划线),STYLE_DASHDOTDOT,STYLE_DOT(点),STYLE_NULL,STYLE_SOLID(实线)
					new dojo.Color([ 255, 0, 0 ]), 0.1), // 颜色 1 // 像素 ),
			new dojo.Color([ 255, 0, 0, 1 ]) // 颜色和透明度
	);
	
	var myPoint = {
		"geometry" : {
			"type" : "point",
			"x" : data.longitude,
			"y" : data.latitude,
			"spatialReference" : {
				"wkid" : 4326
			}
		},
		"attributes" : data,
		"symbol" : symbol,
		"pointType" : str
	};
	
	
	var graphic = getcurrentGraphic("f");
	if (graphic == null) {
		graphic = new esri.Graphic(myPoint);
		graphic.symbol = symbol;
		graphic.pointType = str;
		graphic.showType = "f";
	} else {
		if(str == 'android' || str == graphic.pointType){
			var geometry = graphic.geometry;
			geometry.x = data.longitude;
			geometry.y = data.latitude;
			
			graphic.pointType = str;
			graphic.setGeometry(geometry);
			graphic.setSymbol(symbol);
		}
	}
	eventMap.graphics.add(graphic);
	flickerPoint = graphic;
	createLocationPoint(data, str);
	return graphic;
};

function createLocationPoint(data, str) {
	var symbol = new esri.symbol.PictureMarkerSymbol(defaultPointIcon, 20, 20).setOffset(0,10);
	var myPoint = {
			"geometry" : {
				"type" : "point",
				"x" : data.longitude,
				"y" : data.latitude,
				"spatialReference" : {
					"wkid" : 4326
				}
			},
			"symbol" : symbol,
			"pointType" : str
	};
	
	var graphic = getcurrentGraphic("l");
	if (graphic == null) {
		graphic = new esri.Graphic(myPoint);
		graphic.symbol = symbol;
		graphic.pointType = str;
		graphic.showType = "l";
	} else {
		if(str == 'android' || str == graphic.pointType){
			var geometry = graphic.geometry;
			geometry.x = data.longitude;
			geometry.y = data.latitude;
			
			graphic.pointType = str;
			graphic.setGeometry(geometry);
			graphic.setSymbol(symbol);
		}
	}
	eventMap.graphics.add(graphic);
	locationPoint = graphic;
	
	return graphic;
};

function setCenter() {
	eventMap.centerAt(flickerPoint.geometry);
}

function getcurrentGraphic(type) {
	var graphics;
	graphics = eventMap.graphics.graphics;
	for (var i = 0; i < graphics.length; i++) {
		if (graphics[i]) {
			if (graphics[i].showType && graphics[i].showType == type) {
				return graphics[i];
			}
		}
	}
	return null;
};

			
//createFlickerPoint(data, str);
//setTimeout(function() {
//	setCenter(str);
//}, 500);

